package com.ywq.wechatordersys.service.impl;

import com.ywq.wechatordersys.constant.ErrorCode;
import com.ywq.wechatordersys.data.ProductInfo;
import com.ywq.wechatordersys.dto.CartDTO;
import com.ywq.wechatordersys.enums.ProductStatusEnum;
import com.ywq.wechatordersys.exception.MyException;
import com.ywq.wechatordersys.repository.ProductInfoRepository;
import com.ywq.wechatordersys.service.ProductInfoService;
import com.ywq.wechatordersys.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {


    @Autowired
    private ProductInfoRepository productInfoRepository;

    /**
     * 添加商品
     *
     * @param productInfo
     */
    @Override
    public ProductInfo addProduct(ProductInfo productInfo) {

        if (productInfo.getProductId() == null) {
            //生成id
            String id = IDUtils.createID();
            productInfo.setProductId(id);
        }
        ProductInfo save = productInfoRepository.save(productInfo);

        return save;
    }

    /**
     * 删除商品
     *
     * @param id
     */
    @Override
    public ProductInfo delProduct(String id) {

        Optional<ProductInfo> byId = productInfoRepository.findById(id);
        ProductInfo productInfo = byId.get();
        productInfoRepository.deleteById(id);
        return productInfo;
    }

    /**
     * 修改商品 提前判断id
     *
     * @param productInfo
     */
    @Override
    public ProductInfo editProduct(ProductInfo productInfo) {

        ProductInfo save = productInfoRepository.save(productInfo);
        return save;
    }

    /***
     * 加库存
     * @param id
     * @param stock
     */
    @Override
    public ProductInfo plushStock(String id, Integer stock) {
        return editStock(id, stock);
    }

    /**
     * 减库存
     *
     * @param id
     * @param stock
     */
    @Override
    public ProductInfo minusStock(String id, Integer stock) {
        return editStock(id, -stock);
    }

    @Override
    @Transactional
    public void addStock(List<CartDTO> cartDTOList) {


        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = plushStock(cartDTO.getProductId(), cartDTO.getProductQuantity());
            if (productInfo == null) {
                throw new MyException(ErrorCode.PRODUCT_NO_STOCK, "库存异常");
            }
        }

    }

    @Override
    @Transactional
    public void minStock(List<CartDTO> cartDTOS) {
        for (CartDTO cartDTO : cartDTOS) {
            ProductInfo productInfo = minusStock(cartDTO.getProductId(), cartDTO.getProductQuantity());
            if (productInfo == null) {
                throw new MyException(ErrorCode.PRODUCT_NO_STOCK, "库存不足");
            }
        }

    }

    /**
     * 查询商品  单个
     *
     * @param id
     */
    @Override
    public ProductInfo getProduct(String id) {
        Optional<ProductInfo> productInfo = productInfoRepository.findById(id);

        return productInfo.get();
    }

    /**
     * 查询商品  多个
     *
     * @param ids
     */
    @Override
    public List<ProductInfo> getProducts(List<String> ids) {

        List<ProductInfo> productInfos = productInfoRepository.findAllById(ids);

        return productInfos;
    }


    /**
     * 查询商品  分页
     *
     * @param pageable
     * @param
     */
    @Override
    public List<ProductInfo> getProducts(Pageable pageable) {

        Page<ProductInfo> productInfos = productInfoRepository.findAll(pageable);
        List<ProductInfo> content = productInfos.getContent();
        return content;
    }

    /**
     * 查询商品 上架的
     */
    @Override
    public List<ProductInfo> getAllUpProducts() {

        List<ProductInfo> productInfos = productInfoRepository.findByProductStatus(ProductStatusEnum.UP.getCode());


        return productInfos;
    }


    private ProductInfo editStock(String id, Integer stock) {
        Optional<ProductInfo> byId = productInfoRepository.findById(id);
        ProductInfo productInfo = byId.get();

        if (productInfo==null){
            return null;
        }

        Integer productStock = productInfo.getProductStock();
        productStock += stock;
        if (productStock < 0) {
            return null;
        }
        productInfo.setProductStock(productStock);
        ProductInfo save = productInfoRepository.save(productInfo);
        return save;
    }

}
